import 'package:dio/dio.dart';

/// 写法参考 https://github.com/rodion-m/dio_smart_retry/blob/master/lib/src/retry_interceptor.dart
const String addHeadersKey = "addHeaders";
const String addSignKey = "addSign";
const String showErrorToastKey = "showErrorToast";
const String asHttpResponseDataKey = "asHttpResponseData";

extension ExOptions on Options {
  /// 是否添加公共头部
  bool get addHeaders => extra?[addHeadersKey] == true;

  set addHeaders(bool value) {
    extra ??= <String, dynamic>{};
    extra?[addHeadersKey] = value;
  }

  /// 是否添加签名
  bool get addSign => extra?[addSignKey] == true;

  set addSign(bool value) {
    extra ??= <String, dynamic>{};
    extra?[addSignKey] = value;
  }

  /// 是否显示错误toast
  bool get showErrorToast => extra?[showErrorToastKey] == true;

  set showErrorToast(bool value) {
    extra ??= <String, dynamic>{};
    extra?[showErrorToastKey] = value;
  }

  /// 请求结果是否转换成HttpResponseData
  bool get asHttpResponseData => extra?[asHttpResponseDataKey] == true;

  set asHttpResponseData(bool value) {
    extra ??= <String, dynamic>{};
    extra?[asHttpResponseDataKey] = value;
  }
}

extension ExRequestOptions on RequestOptions {
  /// 是否添加公共头部
  bool get addHeaders => extra[addHeadersKey] == true;

  set addHeaders(bool value) => extra[addHeadersKey] = value;

  /// 是否添加签名
  bool get addSign => extra[addSignKey] == true;

  set addSign(bool value) => extra[addSignKey] = value;

  /// 是否显示错误toast
  bool get showErrorToast => extra[showErrorToastKey] == true;

  set showErrorToast(bool value) => extra[showErrorToastKey] = value;

  /// 请求结果是否转换成HttpResponseData
  bool get asHttpResponseData => extra[asHttpResponseDataKey] == true;

  set asHttpResponseData(bool value) {
    extra[asHttpResponseDataKey] = value;
  }
}
